import { config, Base_Api } from '../../config/index';

/** 获取商品列表 */
function mockFetchGoodsList(pageIndex = 1, pageSize = 20) {
  const { delay } = require('../_utils/delay');
  const { getGoodsList } = require('../../model/goods');
  return delay().then(() =>
    getGoodsList(pageIndex, pageSize).map((item) => {
      return {
        spuId: item.spuId,
        thumb: item.primaryImage,
        title: item.title,
        price: item.minSalePrice,
        originPrice: item.maxLinePrice,
        tags: item.spuTagList.map((tag) => tag.title),
      };
    }),
  );
}




/** 获取商品列表 */
export function fetchGoodsList(pageIndex = 1, pageSize = 20) {
  if (config.useMock) {
    return mockFetchGoodsList(pageIndex, pageSize);
  }
  return new Promise((resolve) => {
    resolve('real api');
  });
}

/** 获取goods数据 */
export function fetchGoods(data) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: Base_Api + "/get_goods",
      method: "POST",
      data: data,
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}


/** 根据商品类别 获取goods商品 */
export function fetchGoodsByCategoryItemId(data) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: Base_Api + "/get_goods_by_categoryItemId",
      method: "POST",
      data: data,
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })

}
